# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=make
# NOTE: The name of the target executable (_prog_name) MUST remain unchanged
#       when installed, otherwise GNU make's dll load feature in connection
#       with the target executable breaks.  The reason is that the executable
#       name is part of the import library (libgnumake-1.dll.a) used to link
#       customized dll's.
#       General information at https://www.gnu.org/software/make/manual/html_node/Loading-Objects.html#Loading-Objects.
_autotools=no
_prog_name=mingw32-make
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.3
pkgrel=1
pkgdesc="GNU make utility to maintain groups of programs (mingw-w64)"
arch=('any')
url="https://www.gnu.org/software/make"
license=('GPL3')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-gettext")
makedepends=('wget')
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.gz"
        'make-getopt.patch'
        'make-linebuf-mingw.patch'
        'make-4.3_undef-HAVE_STRUCT_DIRENT_D_TYPE.patch'
        'make-4.2.1-Makefile.am-gcc-only-link-libgnumake-1.dll.a.patch'
        'make-check-load-feature.mak'
        'mk_temp.c')
sha256sums=('e05fdde47c5f7ca45cb697e973894ff4f5d79e13b750ed57d7b66d8defc78e19'
            '15a86d5143c9ec6337bdd69fecb7c33c36e4fd925528dc1498b0bc4fbd31b0bb'
            'cbb8c0a1bdd6e8174febce01946bd42da26dfcb73a7338c0a1df89ac6b8e157b'
            '5d71fccedb80e4713ed5a0cf73ca79d66f29894eeb6ad4b02037edecf81c2444'
            '85f8c3c555079026f3debab350c9ee27a717cdd08eca3f8bf9ac58e78544d21d'
            '3d6ece384425ff1c3f691efeb7c97e89bb11825bb6648995552dfcb52afe661d'
            '61d4f57c311cf2e27ccf4d1da847216730f304cf17c5c386721182ffe2d4b1aa')


# helper function
get_exe_dir() {
  if test ${_autotools} = yes; then
     echo "${srcdir}/build-${MINGW_CHOST}"
  else
    echo "${srcdir}/${_realname}-${pkgver}/GccRel"
  fi
}


prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  pushd src
    patch -p1 -i ${srcdir}/make-getopt.patch
    patch -p1 -i ${srcdir}/make-linebuf-mingw.patch
    patch -p2 -i ${srcdir}/make-4.3_undef-HAVE_STRUCT_DIRENT_D_TYPE.patch
  popd

  if test "${_autotools}" = "yes"; then
    patch -p1 -i ${srcdir}/make-4.2.1-Makefile.am-gcc-only-link-libgnumake-1.dll.a.patch
    # 1 - The import library libgnumake-1.dll.a contains the name of the program to be
    #     installed.  This requires autotools to be informed about the installed program
    #     name in case it is different from the standard name 'make' hardcoded in the GNU
    #     Make package.
    # 2 - automake requires hardcoded program names in any Makefile.am, i.e. in the
    #     variable bin_PROGRAMS and the induced variables derived from the program
    #     name.  We use sed to change the program name from 'make' to the customized
    #     name in Makefile.am, if different from 'make'.
    # 3 - After packaging we run a test if the dll load feature for the installed
    #     program works.  See function 'package()'.
    if test ${_prog_name} != make; then
      msg2 "Changing the program name from 'make' to '${_prog_name}' in Makefile.am."
    test -f Makefile.am.orig || mv Makefile.am Makefile.am.orig
    local _prog_name_am=${_prog_name//[^a-zA-Z0-9@]/_}
      sed -e "/bin_PROGRAMS/ { s:\bmake\b:${_prog_name}:g };
              s:\bmake_\(SOURCES\|LDADD\|LDFLAGS\)\b:${_prog_name_am}_\1:g ;
              s:\bEXTRA_make_\([A-Z]\+\):EXTRA_${_prog_name_am}_\1:g ;" \
            Makefile.am.orig > Makefile.am
    # log the changes made to Makefile.am by sed
      diff -u Makefile.am.orig Makefile.am > Makefile.am.diff || true
      cat Makefile.am.diff
    fi
    autoreconf -vfi
  else
    sed "/^set MAKE=gnumake/ s/gnumake/${_prog_name}/" build_w32.bat > build_w32-PKGBUILD.bat
    # make sure that the batch file has CRLF as EOL for reliable processing by cmd
    unix2dos build_w32-PKGBUILD.bat
  fi
}

build() {
  if test "${_autotools}" = "yes"; then
    mkdir -vp build-${MINGW_CHOST}
    cd build-${MINGW_CHOST}
    ../${_realname}-${pkgver}/configure \
      -C \
      --prefix=${MINGW_PREFIX} \
      --host=${MINGW_CHOST} \
      --without-libintl-prefix \
      --without-libiconv-prefix \
      CPPFLAGS=-DMAKE_LOAD \
      CFLAGS=-mthreads \
      ac_cv_dos_paths=yes
    make -j1
  else
    cd ${srcdir}/${_realname}-${pkgver}
    ./build_w32-PKGBUILD.bat --without-guile gcc
  fi
}

check() {
  _exe_dir=$(get_exe_dir)
  msg "Checking if GNU Make's dll loading feature we compiled into ${_prog_name}.exe works ..."
  cd "${srcdir}"
  rm -fv ./*.dll
  if "${_exe_dir}"/${_prog_name}.exe -f ./make-check-load-feature.mak \
      --debug=v \
      CFLAGS="-I${srcdir}/${_realname}-${pkgver}/src" \
      LIBS="${_exe_dir}/libgnumake-1.dll.a"
  then
    msg2 "PASSED"
  else
    error "FAILED"
    exit 1
  fi
}

package() {
  _exe_dir=$(get_exe_dir)
  mkdir -pv "${pkgdir}${MINGW_PREFIX}"/{bin,lib,include}
  cd "${srcdir}/${_realname}-${pkgver}"
  cp -fv "${_exe_dir}/${_prog_name}.exe"                    "${pkgdir}${MINGW_PREFIX}/bin/"
  cp -fv "${_exe_dir}/libgnumake-1.dll.a"                   "${pkgdir}${MINGW_PREFIX}/lib/"
  cp -fv "${srcdir}/${_realname}-${pkgver}/src/gnumake.h"   "${pkgdir}${MINGW_PREFIX}/include/"
}
